package com.mnsq.subject.controller;

import com.mnsq.common.bean.annotation.UserInfoRequired;
import com.mnsq.common.bean.conf.ThreadLocalUserId;
import com.mnsq.common.bean.entity.subject.Subject;
import com.mnsq.common.bean.entity.subject.UserCollect;
import com.mnsq.common.controller.BaseController;
import com.mnsq.common.service.BaseService;
import com.mnsq.subject.bean.condition.CollectCondition;
import com.mnsq.subject.service.OperateService;
import com.mnsq.subject.service.SubjectService;
import matrix.module.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 周俭
 */
@RestController
@RequestMapping("/api/collect")
public class CollectController extends BaseController<UserCollect, BaseService<UserCollect, CollectCondition>, CollectCondition> {

    @Autowired
    private OperateService<UserCollect, CollectCondition> operateService;
    @Autowired
    private SubjectService subjectService;

    @PostMapping("/do")
    @UserInfoRequired(required = true)
    public Result col(@RequestBody CollectCondition c) {
        c.setUserId(ThreadLocalUserId.CURRENT_USER_ID.get());
        Subject subject = subjectService.queryByUnique(c.getUnique());
        UserCollect userCollect = new UserCollect();
        userCollect.setResourceUnique(c.getUnique());
        userCollect.setTitle(c.getTitle());
        userCollect.setType(subject.getType());
        userCollect.setUserId(ThreadLocalUserId.CURRENT_USER_ID.get());
        c.setUnique(null);
        c.setType(null);
        return Result.success(operateService.done(userCollect, c));
    }

    @PostMapping("/undo")
    @UserInfoRequired(required = true)
    public Result disCol(@RequestBody CollectCondition c) {
        c.setUserId(ThreadLocalUserId.CURRENT_USER_ID.get());
        UserCollect userCollect = new UserCollect();
        userCollect.setResourceUnique(c.getUnique());
        userCollect.setUserId(ThreadLocalUserId.CURRENT_USER_ID.get());
        c.setUnique(null);
        return Result.success(operateService.undone(new UserCollect(), c));
    }
}
